// jigsaw.js
Page({
    /**
     * 页面的初始数据
     */
    data: {
        row: 4,
        col: 3,
        maxNum: null,
        stepNum: 0,
        cards: []
    },
    swap: function (arr, idx1, idx2) {
        const temp = arr[idx1]
        arr[idx1] = arr[idx2]
        arr[idx2] = temp
    },
    getJigsawArr: function () {
        const row = this.data.row
        const col = this.data.col

        const length = row * col

        let arr = []
        for (let i = 0; i < length - 1; i++) {
            arr[i] = i + 1
        }
        //随机排序
        arr.sort(function (a, b) {
            return Math.random() - 0.5;
        });

        //最大数放在最后
        arr.push(this.data.maxNum)

        //计算逆序对数
        let coverPairCount = 0
        for (let i = 0; i < length; ++i) {
            for (let j = i + 1; j < length; ++j) {
                if (arr[i] > arr[j])
                    coverPairCount++
            }
        }

        //逆序对数量为奇数，则交换非空格位置一次
        if ((coverPairCount & 1) ==1) {
            console.log(arr)
            if (arr[length - 1] != this.data.maxNum && arr[length - 2] != this.data.maxNum) {
                this.swap(arr, length - 1, length - 2)
            } else {
                this.swap(arr, length - 2, length - 3)
            }
            console.log(arr)
        }

        return arr
    },
    getCards(jigsawArr) {
        const cards = []
        for (let i = 0; i < this.data.row; i++) {
            let row = []
            for (let j = 0; j < this.data.col; j++) {
                const idx = i * this.data.col + j
                row.push({
                    index: idx,
                    num: jigsawArr[idx],
                    isShow: jigsawArr[idx] !== this.data.maxNum
                })
            }
            cards.push(row)
        }
        return cards
    },
    initGame: function () {
        this.setData({
            maxNum: this.data.row * this.data.col
        })
        // 重置时间
        this.selectComponent('#timeCount').timerReset()
        const jigsawArr = this.getJigsawArr(this.data.row, this.data.col)
        const cards = this.getCards(jigsawArr)
        console.log(cards)
        this.setData({
            stepNum: 0,
            cards: cards
        })
    },
    tapCard: function (event) {
        const row = this.data.row
        const col = this.data.col
        const cards = this.data.cards

        const rowIdx = event.currentTarget.dataset.rowIdx
        const colIdx = event.currentTarget.dataset.colIdx

        const leftIdx = colIdx - 1 >= 0 && colIdx - 1 <= col
        const rightIdx = colIdx + 1 >= 0 && colIdx + 1 <= col
        const topIdx = rowIdx - 1 >= 0 && rowIdx - 1 <= row
        const bottomIdx = rowIdx + 1 >= 0 && rowIdx + 1 <= row


        if (colIdx - 1 >= 0 && colIdx - 1 < col && !cards[rowIdx][colIdx - 1].isShow) {
            this.moveCard(cards[rowIdx][colIdx], cards[rowIdx][colIdx - 1])
        } else if (colIdx + 1 >= 0 && colIdx + 1 < col && !cards[rowIdx][colIdx + 1].isShow) {
            this.moveCard(cards[rowIdx][colIdx], cards[rowIdx][colIdx + 1])
        } else if (rowIdx - 1 >= 0 && rowIdx - 1 < row && !cards[rowIdx - 1][colIdx].isShow) {
            this.moveCard(cards[rowIdx][colIdx], cards[rowIdx - 1][colIdx])
        } else if (rowIdx + 1 >= 0 && rowIdx + 1 < row && !cards[rowIdx + 1][colIdx].isShow) {
            this.moveCard(cards[rowIdx][colIdx], cards[rowIdx + 1][colIdx])
        }
    },
    moveCard: function (c1, c2) {
        if (!this.selectComponent('#timeCount').hasInit()) {
            this.selectComponent('#timeCount').timerStart()
        }
        const cards = this.data.cards
        let num1 = c1.num
        let isShow1 = c1.isShow
        let num2 = c2.num
        let isShow2 = c2.isShow
        c1.num = num2
        c1.isShow = isShow2
        c2.num = num1
        c2.isShow = isShow1

        let isGameOver = true
        for (let i = 0; i < this.data.row; i++) {
            for (let j = 0; j < this.data.col; j++) {
                const idx = i * this.data.col + j
                if (cards[i][j].num != idx + 1) {
                    isGameOver = false
                    break
                }
            }
        }

        if (isGameOver) {
            c1.isShow = true
        }

        this.setData({
            cards: cards,
            stepNum: this.data.stepNum + 1
        })

        if (isGameOver) {
            this.selectComponent('#timeCount').timerStop()
            wx.showModal({
                title: '恭喜完成',
                content: '用时：' + this.selectComponent('#timeCount').getTimeCount() + '\r\n 步数：' + this.data.stepNum,
                showCancel: false
            })
        }

    },
    /**
     * 页面加载
     */
    onLoad: function (options) {
        this.initGame()
    }
})
